package com.ibt.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ibt.dao.BugEntryDAO;
import com.ibt.dao.BusinessDAO;
import com.ibt.dao.EmployeeDAO;
import com.ibt.dao.LoginDAO;
import com.ibt.entity.Business;
import com.ibt.entity.Employee;
import com.ibt.entity.Login;
import com.ibt.entity.BugEntry;


@Controller
	@RequestMapping("/login")
	public class LoginController {
	
	@RequestMapping(value="/login.do",method = RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request, ModelAndView model,HttpSession session) {

		String userid=request.getParameter("user");
		String pass=request.getParameter("pass");
		LoginDAO logDao= new LoginDAO();
	
		try {
			Login log=logDao.validatCredentials(userid,pass);
			System.out.println("loginUser is "+log);
			 if(log!=null && log.getUserName().trim().length()>0){
				Business business=new Business();
				BusinessDAO busDAO=new BusinessDAO();
				try {
					business= log.getBusinessId();
					System.out.println("company id for User is "+business.getBusinessname());
					session.setAttribute("business",business);
					session.setAttribute("user",log);
				} catch (Exception e) {
					e.printStackTrace();
				}		
			System.out.println("the session variables of user is:"+session.getAttribute("user"));
			model.setViewName("redirect:/newbug/showall.do");
			return model;
			}
			else {
				String message=" The Userid or password entered  is wrong please enter a valid ";
			
				model.setViewName("/jsp/login.jsp?error="+message);
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return model;

	}
	@RequestMapping(value="/logout.do")
	public ModelAndView logout(HttpServletRequest request,HttpSession session,ModelAndView model) {
		       session = request.getSession(false);
		        if (session != null) {
		            session.invalidate();
		        }
		        model.setViewName("/jsp/login.jsp");
		return model;
      }
	@RequestMapping(value="/setpass.do",method = RequestMethod.POST)
	public ModelAndView setpassword(HttpServletRequest request, ModelAndView model,HttpSession session) {

		String userid=request.getParameter("user");
		String pass=request.getParameter("pass");
		String repass=request.getParameter("repass");
				Business business=new Business();
			
				try {
					Employee emp=EmployeeDAO.getBusinessCustMap(userid);
					business= emp.getBusinessId();
					System.out.println("company id for User is "+business.getBusinessname());
				} catch (Exception e) {
					e.printStackTrace();
				}	

				Login log=new Login("sample proj",userid,pass,"sample designation","Sample Company",business);
				 try {
						LoginDAO.insert(log);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				model.setViewName("/jsp/login.jsp");
	
		return model;

	}
	
	
		

}
